import React, { useState, useRef } from 'react';
import { history, connect } from 'umi';
import { Card, Button, Form } from 'antd';
import { Col, Row, Tag, Tooltip, notification, Popover, Space } from 'antd';
import DrawerCondition from '@/components/DrawerCondition';
import FormCondition from '@/components/FormCondition';
import Uploadjs from '@/utils/aliOssUpload';
import { source, sourceSet } from '@/common/contact';
import SelectTagFrom from '@/components/SelectTagForm';
import GoodsSelectComponents from './components/GoodsSelectComponents';
import { phoneReg } from '@/common/regular';
import { useEffect } from 'react';
import { SearchOutlined } from '@ant-design/icons'
import { dateDisabled } from '@/common/regular'
import PreApplition from './components/PreApplition';
import UserFieldRecordModel from '../UserFieldRecordModel';
import { filterStageInfo } from '@/utils/utils'
import moment from 'moment'
const CodeInfo = (props) => {
  const { dispatch, visible = {}, onClose, handleCallback, loading, editMobileStatus = false } = props;
  const { location } = history;
  const { query } = location;
  const { show = false, detail = {} } = visible;
  const [stageAdminStatus, setStageAdminStatus] = useState('system')
  const [form] = Form.useForm();
  const preRef = useRef(null);
  const { stageAdminType = 'system', id, stageAdminList = [], } = detail;
  useEffect(() => {
    if (!show) {
      setStageAdminStatus('system')
    }
    else {

    }
  }, [show])


  const fetchRoleEdit = () => {
    form.validateFields().then(async (values) => {
      dispatch({
        type: 'user/fakeSetUserStageAdmin',
        payload: {
          ...values,
          stageAdminList: stageAdminStatus === 'manual' ? preRef.current.getPre() : [],
          id
        },
        callback: () => {
          onClose();
          handleCallback();
        },
      });
    });
  };

  const modalProps = {
    title: `设置阶段员工 `,
    visible: show,
    onClose,
    destroyOnClose: true,
    footer: (
      <Button type="primary" loading={loading} onClick={fetchRoleEdit}>
        确定
      </Button>
    ),
  };

  const formItems = [
    {
      label: '用户负责人指定',
      name: 'stageAdminType',
      type: 'radio',
      onChange: (e) => {
        setStageAdminStatus(e.target.value)
      },
      select: {
        system: '系统默认',
        manual: '手动指定',
      }
    },
    {
      type: 'noForm',
      visible: stageAdminStatus === 'system',
      formItem: (
        <Button type='link' onClick={() => {
          const url = history.createHref({
            pathname: '/userModule/stageAdminList',
            // 如果需要传递参数，可以在这里添加 state 或 search 属性
          });
          window.open(
            `/#/userModule/stageAdminList?onlyId=USeAJKwK`,
          );
          // history.push({
          //   pathname: ''
          // })
        }}>查看我的系统默认阶段员工 </Button>
      ),
    },
    {
      type: 'noForm',
      visible: stageAdminStatus === 'delay',
      formItem: (
        <div>请尽快设置，24小时候后系统会自动分配</div>
      ),
    },


    {
      type: 'noForm',
      visible: stageAdminStatus === 'manual',
      formItem: (
        <PreApplition
          detaultData={detail}
          ref={preRef}
          defaultList={stageAdminList.reduce((item, val) => {
            const { stage, adminInfoList = [] } = val
            return [...item, ...adminInfoList.map(o => {
              const { adminName } = o
              return {
                ...o,
                stage,
                username: adminName
              }
            })]
          }, [])} ></PreApplition>
      ),
    }
  ];

  return (
    <>
      <DrawerCondition {...modalProps}>
        <FormCondition form={form} formItems={formItems} initialValues={{ stageAdminType }}></FormCondition>
      </DrawerCondition>
    </>
  );
};
export default connect(({ video, loading }) => ({
  loading: loading.models.video,
}))(CodeInfo);
